#include "stdafx.h"
#include "irender/settings.h"
#include "profile/regprofile.h"
#include "SettingsDlg.h"

namespace irender {

settings::settings(bool read_reg)
{
	any_format();
	alpha = false;
	field = field_off;
	if (read_reg) 
		read_registry();
}

settings::~settings()
{
}

void settings::read_registry()
{
	regprofile profile(HKEY_CURRENT_USER, "Software\\Pixmart\\irender", false, true);
	if (!profile.is_open())
		return;
	if (!profile.query("format", format))
		any_format();
	profile.get("alpha", alpha, false);
	if (!profile.query<int>("field", (int&)field) || (unsigned)field > field_lower_first)
		field = field_off;

	encoder.close();
	string encoder_name;
	if (profile.query("encoder", encoder_name)) {
		img::avi::codec_id id;
		id = encoder_name;
		encoder.open(encoder_info, id.fccHandler);
		img::avi::state st;
		if (profile.query(encoder_name + "\\state", st))
			encoder.set_state(st);
	}
}

void settings::read_encoder_state(img::avi::coder & e)
{
	assert(e.is_open());
	regprofile profile(HKEY_CURRENT_USER, "Software\\Pixmart\\irender", false, true);
	if (!profile.is_open())
		return;

	string encoder_name = e.info()->id.code;
	img::avi::state st;
	if (profile.query(encoder_name + "\\state", st))
		e.set_state(st);
}

void settings::write_registry()
{
	regprofile profile(HKEY_CURRENT_USER, "Software\\Pixmart\\irender", true, true);
	if (!profile.is_open())
		return;
	profile.set("format", format);
	profile.set("alpha", alpha);
	profile.set<int>("field", field);

	if (encoder.is_open()) {
		string encoder_name(encoder_info.id.code);
		profile.set("encoder", encoder_name);
		img::avi::state st;
		encoder.state(st);
		profile.set(encoder_name + "\\state", st);
	}
}

void settings::configure(bool write_reg, HWND hParent)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	SettingsDlg dlg(*this, CWnd::FromHandle(hParent));
	if (dlg.DoModal() == IDOK && write_reg)
		write_registry();
}

void settings::any_format()
{
	//img::ext_vec v;
	//img::our_gdiplus_multiwriter().formats(v);
	//format = v[0];
	format = "bmp";
}

} //namespace irender
